#!/bin/bash

function rootfs_install()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	local TMPROOTFS=tmprootfs
	mkdir -p ${TMPROOTFS}/opt/rk/
	mkdir -p ${TMPROOTFS}/usr/share/applications/
	mkdir -p ${TMPROOTFS}/etc/xdg/autostart/

	cp ./gpio.sh                           ${TMPROOTFS}/opt/rk/
	cp ./RKPE3AN2_p2888-0008_gpio_conf.txt ${TMPROOTFS}/opt/rk/gpio_conf.txt
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER2}:${SAMBA_SHARE_DIR2}/sxx/RK_3C/power_management_client  \
											${TMPROOTFS}/opt/rk/
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER2}:${SAMBA_SHARE_DIR2}/sxx/RK_3C/pm_client.sh \
											${TMPROOTFS}/opt/rk/
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER2}:${SAMBA_SHARE_DIR2}/sxx/RK_3C/pm_client.desktop \
											${TMPROOTFS}/usr/share/applications/
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER2}:${SAMBA_SHARE_DIR2}/sxx/RK_3C/pm_client.desktop \
											${TMPROOTFS}/etc/xdg/autostart/
	local TMPROOTFS_PATH=$(abs_path "${TMPROOTFS}")
	local TMPROOTFS_PATH_LEVEL=$(abs_path_level "${TMPROOTFS_PATH}")
	traverse ${TMPROOTFS_PATH} traverse_filter_none 0 traverse_exec_install ${TMPROOTFS_PATH_LEVEL} ${L4T_TOP}/rootfs
	rm -rf ${TMPROOTFS}
}

function rootfs_uninstall()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	sudo_cmd "rm -rf  ${L4T_TOP}/rootfs/opt/rk"
	sudo_cmd "rm ${L4T_TOP}/rootfs/usr/share/applications/pm_client.desktop"
	sudo_cmd "rm ${L4T_TOP}/rootfs/etc/xdg/autostart/pm_client.desktop"
}

#############################################################

if [[ $# -le 0 ]]; then
	exit 0
fi

case $1 in
	"install" )
		rootfs_install
		;;
	"uninstall" )
		rootfs_uninstall
		;;
esac
